<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>AZsPCs - Factorials</title>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script type="text/javascript">
function fact(x) { if (x < 3) return 2; else return x * fact(x - 1);}
function gcd(a,b) { if(b===0) return a; else return gcd(b, a%b);}

function betaSplit(num, list) {
	if (num === 1) {
		return;
	} else if (list.some(function(item) { return item === num; })) {
		return;
	} else if (isCreatable(num, list)) {
		list.push(num);
	} else {
		var res = getFactors(num);
		betaSplit(res.A, list);
		if (!list.some(function(item) { return item === res.A; }))
		list.push(res.A);
		if (res.B !== undefined) {
			betaSplit(res.B, list);
			if (!list.some(function(item) { return item === res.B; }))
			list.push(res.B);
		}
	}
}
	
function doSplit(num, list) {
	var res = getFactors(num);

	if (!list.some(function(item) { return item === res.A; })) {
		if (!isCreatable(res.A, list)) {
			doSplit(res.A, list);
		}
		list.push(res.A);
	}

	if (res.B === undefined) {
		return;
	} else if (!list.some(function(item) { return item === res.B; })) {
		if (!isCreatable(res.B, list)) {
			doSplit(res.B, list);
		}
		list.push(res.B);
	}
}
	
function getFactors(num) {
	var factA, factB;
	
	//HACK: i don't want to handle for primes
	if (num === 7) {
		factA = 3;
		factB = 4;
	} else if (num === 11) {
		factA = 6;
		factB = 5;
	} else if (num === 13) {
		factA = 8;
		factB = 5;
	} else {
		factA = Math.ceil(Math.sqrt(num))
		for (var i = factA; i < num; ++i) {
			if (gcd(num, i) === i) {
				factA = i;
				factB = num / factA;
				break;
			}
		}
	}
	return { A : factA, B : factB };
}

function isCreatable(n, list) {
	var retVal = false;

	for (var j = 0; j < list.length; ++j) {
		for (var k = 0; k < list.length; ++k) {
			if (
				(n === list[j] + list[k]) ||
				(n === list[j] * list[k]) ||
				(n === list[j] - list[k]) ||
				(n === list[k] - list[j])
				) {
				retVal = true;
				break;
			}
		}
		if (retVal) {
			break;
		}
	}

	return retVal;
}

function solve(n) {
	var factorials = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,'1124000727777607680000',
'25852016738884976640000',
'620448401733239439360000',
'15511210043330985984000000',
'403291461126605635584000000',
'10888869450418352160768000000',
'304888344611713860501504000000',
'8841761993739701954543616000000',
'265252859812191058636308480000000',
'8222838654177922817725562880000000',
'263130836933693530167218012160000000',
'8683317618811886495518194401280000000',
'295232799039604140847618609643520000000',
'10333147966386144929666651337523200000000',
'371993326789901217467999448150835200000000',
'13763753091226345046315979581580902400000000'];
				
				
	var end = parseInt(factorials[n]);
	var list = [1];

	//doSplit(end, list);
	betaSplit(end, list);

	list.push(factorials[n]);
	console.log(n, factorials[n], list.length);
	console.log(list.join(','));
}

$(function() {


});
		</script>

	</head>
	<body>
		
	</body>
</html>